<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="4" :sm="4">
            <a-form-item label="订单号">
              <a-input v-model="queryParam.PlanId" placeholder="订单号" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="4">
            <a-form-item label="工序号">
              <a-input v-model="queryParam.OprSequenceNo" placeholder="工序号" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="4">
            <a-form-item label="工作中心">
              <a-input v-model="queryParam.WorkCenter" placeholder="工作中心" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="4">
            <a-form-item label="工位">
              <a-input v-model="queryParam.WorkStation" placeholder="工位" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="4">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" v-action:Query @click="()=>{this.$refs.table.refresh()}">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table ref="table" size="default" rowKey="Id" :columns="columns" :data="loadData" :rowSelection="rowSelection" showPagination="auto">
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="record.ProgressStatus==='120' || record.ProgressStatus==='120.0'" v-action:Update @click="handleEdit(record)">改派工位</a>
        </template>
      </span>
    </s-table>
    <EditForm ref="editForm" @Success="()=>{this.$refs.table.refresh()}"></EditForm>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import EditForm from './Edit'
import MainSvc from '@/api/Exec/OprSequenceSvc'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'

const columns = [
  { title: '订单号', dataIndex: 'PlanId', sorter: true },
  { title: '工序号', dataIndex: 'OprSequenceNo', sorter: true },
  { title: '工序名称', dataIndex: 'OprSequenceName', sorter: true },
  { title: '计划开始时间', dataIndex: 'ScheduledStartDate', sorter: true, customRender: (value) => { return moment(value).format('yyyy-MM-DD') } },
  { title: '工作中心', dataIndex: 'WorkCenterName', sorter: true },
  { title: '工位', dataIndex: 'WorkStationName', sorter: true },
  { title: '工序状态', dataIndex: 'ProgressStatus', sorter: true, customRender: (value) => {
    if (value === '110') {
      return '已下达'
    } else if (value === '120' || value === '120.0') {
      return '已派工'
    } else if (value === '121') {
      return '派工失败'
    } else if (value === '125') {
      return '已接收'
    } else if (value === '130') {
      return '进行中'
    } else if (value === '135') {
      return '暂停'
    } else if (value === '140') {
      return '已完工'
    } else if (value === '150') {
      return '已报工'
    } else if (value === '155') {
      return '报工失败'
    } else if (value === '160') {
      return '取消报工'
    } else if (value === '170') {
      return '已删除'
    } else {
      return value
    }
  } },
  { title: '计划数量', dataIndex: 'Quantity', sorter: true },
  { title: '完成数量', dataIndex: 'ActQuantity', sorter: true },
  { title: '操作', dataIndex: 'action', width: '90px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    STable,
    MainSvc,
    EnumSelect,
    EnumName,
    EditForm
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: { PlanId: '', OprSequenceNo: '', WorkCenter: '', WorkStation: '' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        var _query = Object.assign({}, { ...this.queryParam })
        for (const key in _query) {
          if (moment.isMoment(_query[key])) {
            _query[key] = _query[key].format('YYYY-MM-DD')
          }
        }
        const requestParameters = Object.assign({ sortField: 'ModifyTime', sortOrder: 'desc', Search: _query }, parameter)
        console.log('loadData request parameters:', requestParameters)
        return MainSvc.GetPage(requestParameters)
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    moment,
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
      this.$refs.editForm.openForm(record.Id, record.WorkCenter, '修改')
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm() {
      this.queryParam = { PlanId: '', OprSequenceNo: '', WorkCenter: '', WorkStation: '' }
    }
  }
}
</script>
